/**
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 */
package org.jiopi.ibean.kernel.util;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;

/**
 * 
 * Merger the specified export classes to package if it is possible .
 * <P>
 * For example :<br/>
 * The specified exporting classes are :
 * <UL>
 *     <LI>org.example.a.b.Class1</LI>
 *     <LI>org.example.a.b.Class2</LI>
 *     <LI>org.example.a.c.Class3</LI>
 *     <LI>org.example.a.c.Class4</LI>
 *     <LI>org.example.z.Class5</LI>
 *     <LI>org.example.z.CLass6</LI>
 *     <LI>org.example.x.CLass7</LI>
 * </UL>
 * and the others are :
 * <UL>
 *     <LI>org.example.x.CLass8</LI>
 *     <LI>org.example.x.CLass9</LI>
 * </UL>
 * 
 * The exporting classes should be merged to packages below :
 * <UL>
 *     <LI>org.example.a.**</LI>
 *     <LI>org.example.z.*</LI>
 *     <LI>org.example.x.CLass7</LI>
 * </UL>
 * 
 * </P>
 * 
 * 
 * @since 2010.5.11
 *
 */
public class PackageMerger {
	
	private Package rootPackage = new Package();
	
	public PackageMerger(){
		
	}
	
	public void addClass(String className,boolean export,boolean reset){
		String[] classNamePath = className.split("\\.");
		rootPackage.add(classNamePath, export, 0,reset);
	}
	
	/**
	 * return the merged packages 
	 * 
	 * @return
	 */
	public String[] getExportPackages(){
		ArrayList<String> pacakges = rootPackage.getExportPackages();
		ArrayList<String> newPacakges = new ArrayList<String>();
		for(String s : pacakges){
			if(!"-".equals(s)){
				newPacakges.add(s);
			}
		}
		
		return newPacakges.toArray(new String[newPacakges.size()]);
	}
	
	private class Package{
		private LinkedHashMap<String,Package> subPackages = new LinkedHashMap<String,Package>();
		private LinkedHashSet<String> exportClasses = new LinkedHashSet<String>();
		private LinkedHashSet<String> otherClasses = new LinkedHashSet<String>();
		public Package(){
		}
		
		/**
		 * 
		 * @param className
		 * @param export
		 * @param index
		 * @param reset   reset export type if have been set
		 */
		public void add(String[] className,boolean export,int index,boolean reset){
			if(index>=className.length) return;
			if(index+1==className.length){//add class
				LinkedHashSet<String> classesSet = null;
				LinkedHashSet<String> anotherClassesSet = null;
				if(export){
					classesSet = exportClasses;
					anotherClassesSet = otherClasses;
				}
				else {
					classesSet = otherClasses;
					anotherClassesSet = exportClasses;
				}
				if(reset){
					classesSet.remove(className[index]);
					anotherClassesSet.remove(className[index]);
				}
				if(!anotherClassesSet.contains(className[index])) classesSet.add(className[index]);
			}else{//add package
				Package subPackage = subPackages.get(className[index]);
				if(subPackage == null){
					subPackage = new Package();
					subPackages.put(className[index], subPackage);
				}
				subPackage.add(className,export,++index,reset);
			}
		}
		
		public ArrayList<String> getExportPackages(){
			
			ArrayList<String> pacakges = new ArrayList<String>();
			//first,get exporting packages form sub
			String exportAllSubPackages = "";
			ArrayList<String> subExportPacakges = new ArrayList<String>();
			if(subPackages.size()==0){
				exportAllSubPackages="*";
			}else{
				int exportAllCount = 0;
				for(String packageName:subPackages.keySet()){
					Package subPackage = subPackages.get(packageName);
					ArrayList<String> aExportPacakge = subPackage.getExportPackages();
					int exprotSubCount = 0;
					for(String name:aExportPacakge){
						if(name.endsWith("*")){
							exprotSubCount++;
						}
						//System.out.println(packageName+"."+name);
						if(!"-".equals(name)){
							subExportPacakges.add(packageName+"."+name);
						}else{
							subExportPacakges.add("-");
						}
					}
					if(exprotSubCount>0 && exprotSubCount == aExportPacakge.size()){
						exportAllCount ++;
					}
				}
				if(exportAllCount>1&&exportAllCount == subPackages.size()){
					exportAllSubPackages="*";
				}

			}
			//second,get exporting classes
			String exportAllSubClasses = "";
			ArrayList<String> exportClassePacakges = new ArrayList<String>();
			if(otherClasses.size()==0){//exports all
				exportAllSubClasses = "*";
			}else if(exportClasses.size()>0){
				for(String className:exportClasses){
					exportClassePacakges.add(className);
				}
			}else{
				exportClassePacakges.add("-");
			}
			
			
			if(subPackages.size()>0&&"*".equals(exportAllSubPackages)&&"*".equals(exportAllSubClasses)){
				pacakges.add("**");
			}
			else{
				//add sub packages
				pacakges.addAll(subExportPacakges);
				
				//add sub classes
				if("*".equals(exportAllSubClasses)&&exportClasses.size()>0){
					pacakges.add("*");
				}else if(exportClassePacakges.size()>0){
					pacakges.addAll(exportClassePacakges);
				}else{
					pacakges.add("-");
				}
			}
			
			//merge exporting packages and classes 
			return pacakges;
		}
	}
}
